﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Antlr.Runtime;
using Antlr.Runtime.Tree;

namespace Expression
{

    public class Exp
    {
        public ANTLRStringStream stringInput { get; set; }
        public CombinedParser parser { get; set; }
        public CombinedLexer lexer { get; set; }
        public List<char> Parameters { get; set; }
        public List<char> Vars { get; set; }

        public Exp() { }

        public Exp(string input,List<char> vars)
        {
            stringInput = new ANTLRStringStream(input);
            Vars = vars;

            lexer = new CombinedLexer(stringInput);
            CommonTokenStream Tokens = new CommonTokenStream(lexer);
            parser = new CombinedParser(Tokens);
            parser.expression();
            Parameters = parser.Parameters;
            List<char> tmpChars = new List<char>();
            foreach (char c in Parameters)
            {
                if (Vars.Contains(c))
                    tmpChars.Add(c);
            }
            foreach (char c in tmpChars)
                Parameters.Remove(c);
            if (Parameters.Count > 0)
            {
                GC.SuppressFinalize(this);
                throw new Exception(string.Format("Cant support this expression: {0} parameter(s)", Parameters.Count));
            }
        }

        public string getFactor(char var)
        {
            string factor = string.Empty;

            return factor;
        }

        public double Value
        {
            get
            {
                double v = 0;
                parser.expression();
                if (parser.Parameters.Count > 0)
                    throw new Exception(string.Format("Cant support this expression: {0} parameter(s)", Parameters.Count));
                else
                    v = parser.mult().value ;
                return v;
            }
        }
    }
}
